
<%@ page import="ch.vd.dec.selt.petale.TypologieEmolument" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <title><g:message code="typologieEmolument.edit" default="Edit TypologieEmolument" /></title>
		<g:javascript library="prototype" />
		
		<g:javascript>

			function updateSelect() {
				
				var zselectBase = document.getElementById('base.id') 
				var zoptBase = zselectBase.options[zselectBase.selectedIndex] 
				
				var zselectAut = document.getElementById('aut.id') 
				var zoptAut = zselectAut.options[zselectAut.selectedIndex] 
				
				var zselectCat = document.getElementById('cat.id') 
				var zoptCat = zselectCat.options[zselectCat.selectedIndex] 
				
				var zselectSubCat = document.getElementById('subcat.id') 
				var zoptSubCat = zselectSubCat.options[zselectSubCat.selectedIndex] 
				
				var zselectPre = document.getElementById('pre.id') 
				var zoptPre = zselectPre.options[zselectPre.selectedIndex] 
				
				document.getElementById('uniqueKey').value = \
					((zoptBase.value != 'null' && zoptBase.value != '') ? zoptBase.value :0) + '#' + \
					((zoptAut.value != 'null' && zoptAut.value != '') ? zoptAut.value :0) + '#' + \
					((zoptCat.value != 'null' && zoptCat.value != '') ? zoptCat.value :0)  + '#' + \
					((zoptSubCat.value != 'null' && zoptSubCat.value != '') ? zoptSubCat.value :0) + '#' + \
					((zoptPre.value != 'null' && zoptPre.value != '') ? zoptPre.value :0)

				${remoteFunction(controller:"typologiePrestation", action:"ajaxGetBaseLegale", params:"'aut.id=' + zoptAut.value + '&cat.id=' + zoptCat.value + '&subcat.id=' + zoptSubCat.value + '&pre.id=' + zoptPre.value", onComplete:"updateTypologie(e, 'base.id', zoptBase.value)")};
				${remoteFunction(controller:"typologiePrestation", action:"ajaxGetTypeAutorisation", params:"'base.id=' + zoptBase.value + '&cat.id=' + zoptCat.value + '&subcat.id=' + zoptSubCat.value + '&pre.id=' + zoptPre.value", onComplete:"updateTypologie(e, 'aut.id', zoptAut.value)")};
				${remoteFunction(controller:"typologiePrestation", action:"ajaxGetCategorie", params:"'base.id=' + zoptBase.value + '&aut.id=' + zoptAut.value + '&subcat.id=' + zoptSubCat.value + '&pre.id=' + zoptPre.value", onComplete:"updateTypologie(e, 'cat.id', zoptCat.value)")};
				${remoteFunction(controller:"typologiePrestation", action:"ajaxGetSousCategorie", params:"'base.id=' + zoptBase.value + '&aut.id=' + zoptAut.value + '&cat.id=' + zoptCat.value + '&pre.id=' + zoptPre.value", onComplete:"updateTypologie(e, 'subcat.id', zoptSubCat.value)")};
				${remoteFunction(controller:"typologiePrestation", action:"ajaxGetPrestation", params:"'base.id=' + zoptBase.value + '&aut.id=' + zoptAut.value + '&cat.id=' + zoptCat.value + '&subcat.id=' + zoptSubCat.value", onComplete:"updateTypologie(e, 'pre.id', zoptPre.value)")};
			}
			
			function updateTypologie(e, field, sel) { // The response comes back as a bunch-o-JSON 
				
				var typos = eval("(" + e.responseText + ")") // evaluate JSON

				if (typos) { 
					var rselect = document.getElementById(field)
					
					// Clear all previous options 
					var l = rselect.length
					while (l > 0) { 
						rselect.remove(l) 
						l-- // placé après pour laisser l'élément null en premier
					}
				
					// Rebuild the select
					for (var i=0; i < typos.length; i++) {
						var typo = typos[i] 
						var opt = document.createElement('option'); 
						opt.text = typo.name 
						opt.value = typo.id 
						try { 
							rselect.add(opt, null) // standards compliant; doesn't work in IE 
						} catch(ex) { 
							rselect.add(opt) // IE only 
						} 
						if (typo.id==sel) {
							rselect.selectedIndex=i+1; // +1 car éléments null en première position
						}
					}
				} 
			}
		</g:javascript>
    </head>
    <body>
        <div class="nav">
            <span class="menuButton"><a class="home" href="${createLinkTo(dir: '')}"><g:message code="home" default="Home" /></a></span>
            <span class="menuButton"><g:link class="list" action="list"><g:message code="typologieEmolument.list" default="TypologieEmolument List" /></g:link></span>
            <span class="menuButton"><g:link class="create" action="create"><g:message code="typologieEmolument.new" default="New TypologieEmolument" /></g:link></span>
        </div>
        <div class="body">
            <h1><g:message code="typologieEmolument.edit" default="Edit TypologieEmolument" /></h1>
            <g:if test="${flash.message}">
            <div class="message"><g:message code="${flash.message}" args="${flash.args}" default="${flash.defaultMessage}" /></div>
            </g:if>
            <g:hasErrors bean="${typologieEmolumentInstance}">
            <div class="errors">
                <g:renderErrors bean="${typologieEmolumentInstance}" as="list" />
            </div>
            </g:hasErrors>
            <g:form method="post" >
                <g:hiddenField name="id" value="${typologieEmolumentInstance?.id}" />
                <g:hiddenField name="version" value="${typologieEmolumentInstance?.version}" />
                <div class="dialog">
                    <table>
                        <tbody>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="base"><g:message code="typologieEmolument.base" default="Base" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologieEmolumentInstance, field: 'base', 'errors')}">
                                    <g:select 
										name="base.id" 
										from="${ch.vd.dec.selt.petale.BaseLegale.list()}" 
										optionKey="id" 
										value="${typologieEmolumentInstance?.base?.id}" 
										noSelection="['null': '']" 
										onchange="updateSelect()"/>

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="aut"><g:message code="typologieEmolument.aut" default="Aut" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologieEmolumentInstance, field: 'aut', 'errors')}">
                                    <g:select name="aut.id" from="${ch.vd.dec.selt.petale.TypeAutorisation.list()}" optionKey="id" value="${typologieEmolumentInstance?.aut?.id}" noSelection="['null': '']" onchange="updateSelect()"/>

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="cat"><g:message code="typologieEmolument.cat" default="Cat" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologieEmolumentInstance, field: 'cat', 'errors')}">
                                    <g:select name="cat.id" from="${ch.vd.dec.selt.petale.Categorie.list()}" optionKey="id" value="${typologieEmolumentInstance?.cat?.id}" noSelection="['null': '']" onchange="updateSelect()"/>

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="subcat"><g:message code="typologieEmolument.subcat" default="Subcat" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologieEmolumentInstance, field: 'subcat', 'errors')}">
                                    <g:select name="subcat.id" from="${ch.vd.dec.selt.petale.SousCategorie.list()}" optionKey="id" value="${typologieEmolumentInstance?.subcat?.id}" noSelection="['null': '']" onchange="updateSelect()"/>

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="pre"><g:message code="typologieEmolument.pre" default="Pre" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologieEmolumentInstance, field: 'pre', 'errors')}">
                                    <g:select name="pre.id" from="${ch.vd.dec.selt.petale.Prestation.list()}" optionKey="id" value="${typologieEmolumentInstance?.pre?.id}" noSelection="['null': '']" onchange="updateSelect()"/>

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="emo"><g:message code="typologieEmolument.emo" default="Emo" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologieEmolumentInstance, field: 'emo', 'errors')}">
                                    <g:textField name="emo" value="${fieldValue(bean: typologieEmolumentInstance, field: 'emo')}" />

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="emo_saiso"><g:message code="typologieEmolument.emo_saiso" default="Emosaiso" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologieEmolumentInstance, field: 'emo_saiso', 'errors')}">
                                    <g:textField name="emo_saiso" value="${fieldValue(bean: typologieEmolumentInstance, field: 'emo_saiso')}" />

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="priorite"><g:message code="typologieEmolument.priorite" default="Priorite" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologieEmolumentInstance, field: 'priorite', 'errors')}">
                                    <g:select name="priorite" from="${1..32}" value="${typologieEmolumentInstance?.priorite}"  />

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="uniqueKey"><g:message code="typologieEmolument.uniqueKey" default="Unique Key" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologieEmolumentInstance, field: 'uniqueKey', 'errors')}">
                                    <g:textField name="uniqueKey" maxlength="30" value="${fieldValue(bean: typologieEmolumentInstance, field: 'uniqueKey')}" />

                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="buttons">
                    <span class="button"><g:actionSubmit class="save" action="update" value="${message(code: 'update', 'default': 'Update')}" /></span>
                    <span class="button"><g:actionSubmit class="delete" action="delete" value="${message(code: 'delete', 'default': 'Delete')}" onclick="return confirm('${message(code: 'delete.confirm', 'default': 'Are you sure?')}');" /></span>
                </div>
            </g:form>
        </div>
		<g:javascript>
			// This is called when the page loads to initialize
			updateSelect();
		</g:javascript>
    </body>
</html>
